//
//  NetWorkAssistant.m
//  iOSCodeLearning
//
//  Created by TFAppleWork-Summer on 2018/1/11.
//  Copyright © 2018年 Ace. All rights reserved.
//

#import "NetWorkAssistant.h"

@interface NetWorkAssistant()
@property (nonatomic,strong) AFHTTPSessionManager *manager;
@end

@implementation NetWorkAssistant
SingleM(NetWorkAssistant)

-(void)setRequestHeaderWithDict:(NSDictionary *)dict{
    
    // 1 实例化
    self.manager =  [AFHTTPSessionManager  manager];
    // 2 调出请求头
    self.manager.requestSerializer = [AFJSONRequestSerializer serializer];
    
    // 3 设置请求头
    self.manager.requestSerializer = [AFJSONRequestSerializer new];
    self.manager.requestSerializer = [AFJSONRequestSerializer serializer];//请求
    
    NSArray *keys = [dict allKeys];
    
    for (NSInteger i = 0; i< keys.count; i++)
    {
        NSString *key = keys[i];
        [self.manager.requestSerializer setValue:dict[key] forHTTPHeaderField:key];
    }
    
    
    
}

- (void)post:(NSString *)url
  parameters:(NSDictionary *)param
    progress:(void (^)(NSProgress * _Nonnull))progress
     success:(void (^)(id))success
     failure:(void (^)(NSError * _Nonnull))failure{
    
    [self.manager POST:url
            parameters:param
              progress:^(NSProgress * _Nonnull uploadProgress) {
                  if (progress) {
                      progress(uploadProgress);
                  }
              }
               success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                   if (success) {
                       success(responseObject);
                   }
               }
               failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                   failure(error);
               }];
    
    
    
}

-(void)get:(NSString *)url
parameters:(NSDictionary *)param
  progress:(void (^)(NSProgress * _Nonnull))progress
   success:(void (^)(id))success
   failure:(void (^)(NSError * _Nonnull))failure{
    
    [self.manager GET:url
           parameters:param
             progress:^(NSProgress * _Nonnull downloadProgress) {
                 if (progress) {
                      progress(downloadProgress);
                 }
             }
              success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                  if (success) {
                      success(responseObject);
                  }
              }
              failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                  if (failure) {
                      failure(error);
                  }
              }];
}


@end
